package com.shop.cloud.service.hongfu.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.dao.hongfu.ShopDao;
import com.shop.cloud.domain.hongfu.Shop;
import com.shop.cloud.domain.hongfu.ShopImage;
import com.shop.cloud.service.hongfu.IShopService;
import com.shop.cloud.util.MagicConstants;
import com.shop.cloud.util.Objects;
import com.shop.cloud.util.Page;

/**
 * shop
 */
@Service("shopService")
public class ShopService extends BaseService<Shop> implements IShopService{
    
    @Autowired
	private ShopDao shopDao;
    
    @Autowired
    private ShopImageService shopImageService;

	@Override
	public MybatisDao<Shop> getEntityDao() {
		return shopDao;
	}

	public Page<Shop> findMap(Map<String, Object> params) {
		int limitStart=0,limitEnd = 20;
		Object limitStartObj=params.get(MagicConstants.LIMIT_START);
		Object limitEndObj=params.get(MagicConstants.LIMIT_END);
		if(limitStartObj!=null){
			limitStart = Objects.strToInt(limitStartObj.toString(), 0);
		}
		if(limitEndObj!=null){
			limitEnd = Objects.strToInt(limitEndObj.toString(), 20);
		}
		params.put(MagicConstants.LIMIT_START,limitStart);
		params.put(MagicConstants.LIMIT_END,limitEnd);
		long count = shopDao.count(params);
		Page<Shop> page = new Page<Shop>(Integer.parseInt(String.valueOf(params.get("pageIndex"))),Integer.parseInt(String.valueOf(params.get("pageSize"))));
		params.put("limitStart", (Integer.parseInt(String.valueOf(params.get("pageIndex")))-1)*Integer.parseInt(String.valueOf(params.get("pageSize"))));
		params.put("limitEnd", Integer.parseInt(String.valueOf(params.get("pageSize"))));
		List<Shop> list = shopDao.findMap(params);
		if (list!=null) {
			for (Shop shop2 : list) {
				params = new HashMap<String, Object>();
				params.put("shopId", shop2.getId());
				ShopImage shopImage = shopImageService.findOne(params);
				if(shopImage != null) {
	    			shop2.setIcon(shopImage.getImage());
	    		} else {
	    			shop2.setIcon("");
	    		}
			}
		}
		page.setData(list);
		page.setTotalCount(count);
		return page;
	}

	public Page<Shop> allPage(Shop shop) {
		shop.setPageSize((shop.getPageSize_()==null?20:shop.getPageSize_()));
		long count = shopDao.countByEntity(shop);
		Page<Shop> page = new Page<Shop>(shop.getPageIndex_(), shop.getPageSize_());
		//有无所属门店0-无   1-有
		List<Shop> data =shopDao.findByEntity(shop);
		if (data!=null) {
			for (Shop shop2 : data) {
				shop2.setIsShop(1);
			}
		}
		page.setData(data);
		page.setTotalCount(count);
		return page;
	}

}